<?php

require_once 'DAO.php';
require_once '..\vo\UserVO.php';

class UserDAO extends DAO {
     
    public function add($UserVO) {
        
        // super method set_database is called
        $this->set_database();
        
        $name = $UserVO->get_name();
        $user = $UserVO->get_user();
        $pwd = md5('xt%e3'.$UserVO->get_pwd().'6&ui*');
        
        $query = "INSERT INTO user (name, user, pwd) VALUES " .
            "('$name','$user','$pwd')";
        
        return mysql_query($query);
        
    }
    
    public function update($UserVO) {
        
        // super method set_database is called
        $this->set_database();
        
        $id = $UserVO->get_id();
        $name = $UserVO->get_name();
        $user = $UserVO->get_user();
        $pwd = md5('xt%e3'.$UserVO->get_pwd().'6&ui*');
        
        $query = "UPDATE user SET name = '$name', user = '$user', pwd = '$pwd' WHERE id = '$id'";
        
        return mysql_query($query);      
        
    }
    
    public function delete($id) {
        
        // super method set_database is called
        $this->set_database();
        
        $query = "DELETE FROM user WHERE id = '$id'";
        
        mysql_query($query);
        
        return;
        
    }
    
    public function select($id) {
        
        // super method set_database is called
        $this->set_database();
        
        $query = "SELECT * FROM user WHERE id = '$id'";
        
        $result = mysql_query($query);
            
        if($result) {
            
            $rows = mysql_num_rows($result);
            $UserVO = new UserVO();
                
            for($i = 0; $i < $rows; ++$i) {
                
                $row = mysql_fetch_row($result);
                
                $UserVO->set_id($row[0]);
                $UserVO->set_name($row[1]);
                $UserVO->set_user($row[2]);
                $UserVO->set_pwd($row[3]);
                        
            }
                
            return $UserVO;
            
        }
        
    }
    
    public function verifyLogin($UserVO) {
        
        // super method set_database is called
        $this->set_database();
             
        $user = $UserVO->get_user();
        $pwd = $UserVO->get_pwd();
        
        $query = "SELECT * FROM user WHERE user = '$user'";
        
        $result = mysql_query($query);
            
        if($result) {
            
            $rows = mysql_num_rows($result);
                            
            if($rows === 1) {
                $row = mysql_fetch_row($result);               
                if(md5('xt%e3'.$pwd.'6&ui*') === $row[3]) {
                    return true;
                }
                return false;
            } else {
                return false;
            }
                
        }
        
    }
    
    public function listAll() {
        
        // super method set_database is called
        $this->set_database();
                    
        $query = "SELECT * FROM user";
        $result = mysql_query($query);
            
        if($result) {
                
            $rows = mysql_num_rows($result);
            $UserVOs = array();
                
            for($i = 0; $i < $rows; ++$i) {
                
                $row = mysql_fetch_row($result);
                
                $UserVO = new UserVO();
                $UserVO->set_id($row[0]);
                $UserVO->set_name($row[1]);
                $UserVO->set_user($row[2]);
                $UserVO->set_pwd($row[3]);
                    
                $UserVOs[] = $UserVO;
                    
            }
                
            return $UserVOs;
                
        } else {
            return array();
        }
            
    }
                 
}

?>
